package org.gwu.seuss.communication.sockets.ssl;

import java.util.Properties;

public class SystemProperties {

	public final static String KEYSTORE_PROP_KEY = "system.javax.net.ssl.keyStore";
	public final static String KEYSTORE_PASSWORD_KEY = "system.javax.net.ssl.keyStorePassword";
	
	public final static String TRUSTSTORE_PROP_KEY = "system.javax.net.ssl.trustStore";
	public final static String TRUSTSTORE_PASSWORD_KEY = "system.javax.net.ssl.trustStorePassword";
		
	public static void initSystemProperties(Properties systemProperties, char[] keystorePassword, char[] trustStorePassword){

		System.setProperty("javax.net.ssl.trustStorePassword", String.copyValueOf(trustStorePassword));
		System.setProperty("javax.net.ssl.keyStorePassword", String.copyValueOf(keystorePassword));
		
		for (String key : systemProperties.stringPropertyNames()) {
			
			if(key.equals(KEYSTORE_PASSWORD_KEY) || key.equals(TRUSTSTORE_PASSWORD_KEY)){
				continue;
			}
			
			String[] propertyStrings = key.split("\\.", 2);
			
			if(propertyStrings.length == 2){
				
				if(propertyStrings[0].equals("system")){
					
					System.setProperty(propertyStrings[1], systemProperties.getProperty(key));
				}	
			}
		}	
	}

}
